﻿package core.ui.util
{
    import core.event.*;
    import core.ui.controls.*;
    import core.ui.options.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;

    public class LayerStyle extends Dialog
    {
        var cf:Array;
        var bf:Array;
        var holder:Sprite;
        var filtertitle:Literal;
        var dropshadowoptions:ShadowOptions;
        var innershadowoptions:ShadowOptions;
        var outerglowoptions:GlowOptions;
        var innerglowoptions:GlowOptions;
        var beveloptions:BevelOptions;
        var dropshadow:CheckItem;
        var innershadow:CheckItem;
        var outerglow:CheckItem;
        var innerglow:CheckItem;
        var bevel:CheckItem;
        var lci:CheckItem;

        public function LayerStyle(param1:Tuyitu)
        {
            super(param1, "rm_layer_style", 430, 340, false);
            var _loc_2:* = new Sprite();
            _loc_2.graphics.beginFill(9474192);
            _loc_2.graphics.drawRect(0, 0, 134, 260);
            _loc_2.graphics.beginFill(16579836);
            _loc_2.graphics.drawRect(1, 1, 132, 258);
            _loc_2.graphics.endFill();
            _loc_2.x = 10;
            _loc_2.y = 25;
            addChild(_loc_2);
            this.dropshadow = new CheckItem("DROP_SHADOW", U.RM("rm_drop_shadow"));
            this.dropshadow.addEventListener(MouseEvent.MOUSE_DOWN, this.CheckItemMouseDown, false, 1, true);
            this.dropshadow.addEventListener(ChangeEvent.CHANGE, this.ChangeFilterState, false, 1, true);
            this.dropshadow.x = 2;
            this.dropshadow.y = 2;
            _loc_2.addChild(this.dropshadow);
            this.innershadow = new CheckItem("INNER_SHADOW", U.RM("rm_inner_shadow"));
            this.innershadow.addEventListener(MouseEvent.MOUSE_DOWN, this.CheckItemMouseDown, false, 1, true);
            this.innershadow.addEventListener(ChangeEvent.CHANGE, this.ChangeFilterState, false, 1, true);
            this.innershadow.x = 2;
            this.innershadow.y = 23;
            _loc_2.addChild(this.innershadow);
            this.bevel = new CheckItem("BEVEL", U.RM("rm_bevel"));
            this.bevel.addEventListener(MouseEvent.MOUSE_DOWN, this.CheckItemMouseDown, false, 1, true);
            this.bevel.addEventListener(ChangeEvent.CHANGE, this.ChangeFilterState, false, 1, true);
            this.bevel.x = 2;
            this.bevel.y = 44;
            _loc_2.addChild(this.bevel);
            this.outerglow = new CheckItem("OUTER_GLOW", U.RM("rm_outer_glow"));
            this.outerglow.addEventListener(MouseEvent.MOUSE_DOWN, this.CheckItemMouseDown, false, 1, true);
            this.outerglow.addEventListener(ChangeEvent.CHANGE, this.ChangeFilterState, false, 1, true);
            this.outerglow.x = 2;
            this.outerglow.y = 65;
            _loc_2.addChild(this.outerglow);
            this.innerglow = new CheckItem("INNER_GLOW", U.RM("rm_inner_glow"));
            this.innerglow.addEventListener(MouseEvent.MOUSE_DOWN, this.CheckItemMouseDown, false, 1, true);
            this.innerglow.addEventListener(ChangeEvent.CHANGE, this.ChangeFilterState, false, 1, true);
            this.innerglow.x = 2;
            this.innerglow.y = 86;
            _loc_2.addChild(this.innerglow);
            this.holder = new Sprite();
            this.holder.x = 180;
            this.holder.y = 45;
            addChild(this.holder);
            this.filtertitle = new Literal();
            this.filtertitle.x = 150;
            this.filtertitle.y = 27;
            addChild(this.filtertitle);
            this.CheckCurrentFilters();
            return;
        }// end function

        public function CheckCurrentFilters() : void
        {
            var _loc_1:int = 0;
            if (ed.canvas.layer.filters != null && ed.canvas.layer.filters.length > 0)
            {
                this.cf = ed.canvas.layer.filters;
                this.bf = new Array();
                _loc_1 = 0;
                while (_loc_1 < this.cf.length)
                {
                    
                    this.bf.push(this.cf[_loc_1]);
                    if (this.cf[_loc_1] is DropShadowFilter && !(this.cf[_loc_1] as DropShadowFilter).inner)
                    {
                        this.dropshadow.cb.Check(true);
                    }
                    else if (this.cf[_loc_1] is DropShadowFilter)
                    {
                        this.innershadow.cb.Check(true);
                    }
                    else if (this.cf[_loc_1] is BevelFilter)
                    {
                        this.bevel.cb.Check(true);
                    }
                    else if (this.cf[_loc_1] is GlowFilter && !(this.cf[_loc_1] as GlowFilter).inner)
                    {
                        this.outerglow.cb.Check(true);
                    }
                    else if (this.cf[_loc_1] is GlowFilter)
                    {
                        this.innerglow.cb.Check(true);
                    }
                    _loc_1++;
                }
            }
            return;
        }// end function

        public function CheckItemMouseDown(event:MouseEvent) : void
        {
            if (this.lci != null)
            {
                this.lci.Marking(false);
            }
            var _loc_2:* = event.currentTarget as CheckItem;
            _loc_2.Marking(true);
            this.lci = _loc_2;
            if (this.holder.numChildren > 0)
            {
                this.holder.removeChildAt(0);
            }
            this.filtertitle.text = _loc_2.title.text;
            switch(_loc_2.name)
            {
                case "DROP_SHADOW":
                {
                    this.dropshadowoptions = new ShadowOptions(ed, this.GetFilter(_loc_2.name) as DropShadowFilter);
                    this.dropshadowoptions.addEventListener(ChangeEvent.CHANGE, this.FilterChange);
                    this.holder.addChild(this.dropshadowoptions);
                    break;
                }
                case "INNER_SHADOW":
                {
                    this.innershadowoptions = new ShadowOptions(ed, this.GetFilter(_loc_2.name) as DropShadowFilter);
                    this.innershadowoptions.addEventListener(ChangeEvent.CHANGE, this.FilterChange);
                    this.holder.addChild(this.innershadowoptions);
                    break;
                }
                case "OUTER_GLOW":
                {
                    this.outerglowoptions = new GlowOptions(ed, this.GetFilter(_loc_2.name) as GlowFilter);
                    this.outerglowoptions.addEventListener(ChangeEvent.CHANGE, this.FilterChange);
                    this.holder.addChild(this.outerglowoptions);
                    break;
                }
                case "INNER_GLOW":
                {
                    this.innerglowoptions = new GlowOptions(ed, this.GetFilter(_loc_2.name) as GlowFilter);
                    this.innerglowoptions.addEventListener(ChangeEvent.CHANGE, this.FilterChange);
                    this.holder.addChild(this.innerglowoptions);
                    break;
                }
                case "BEVEL":
                {
                    this.beveloptions = new BevelOptions(ed, this.GetFilter(_loc_2.name) as BevelFilter);
                    this.beveloptions.addEventListener(ChangeEvent.CHANGE, this.FilterChange);
                    this.holder.addChild(this.beveloptions);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.FilterChange();
            return;
        }// end function

        public function FilterChange(event:ChangeEvent = null) : void
        {
            ed.canvas.layer.filters = this.cf;
            return;
        }// end function

        public function RemoveFilter(holder:String) : void
        {
            var _loc_2:int = 0;
            while (_loc_2 < this.cf.length)
            {
                
                if (holder == "INNER_SHADOW" && this.cf[_loc_2] is DropShadowFilter && (this.cf[_loc_2] as DropShadowFilter).inner)
                {
                    this.cf.splice(_loc_2, 1);
                }
                else if (holder == "DROP_SHADOW" && this.cf[_loc_2] is DropShadowFilter && !(this.cf[_loc_2] as DropShadowFilter).inner)
                {
                    this.cf.splice(_loc_2, 1);
                }
                else if (holder == "BEVEL" && this.cf[_loc_2] is BevelFilter)
                {
                    this.cf.splice(_loc_2, 1);
                }
                else if (holder == "INNER_GLOW" && this.cf[_loc_2] is GlowFilter && (this.cf[_loc_2] as GlowFilter).inner)
                {
                    this.cf.splice(_loc_2, 1);
                }
                else if (holder == "OUTER_GLOW" && this.cf[_loc_2] is GlowFilter && !(this.cf[_loc_2] as GlowFilter).inner)
                {
                    this.cf.splice(_loc_2, 1);
                }
                _loc_2++;
            }
            return;
        }// end function

        public function GetFilter(flash.display:InteractiveObject:String) : BitmapFilter
        {
            var _loc_2:DropShadowFilter = null;
            var _loc_3:DropShadowFilter = null;
            var _loc_4:BevelFilter = null;
            var _loc_5:GlowFilter = null;
            var _loc_6:GlowFilter = null;
            var _loc_7:int = 0;
            if (this.cf == null)
            {
                this.cf = new Array();
            }
            while (_loc_7 < this.cf.length)
            {
                
                if (flash.display:InteractiveObject == "INNER_SHADOW" && this.cf[_loc_7] is DropShadowFilter && (this.cf[_loc_7] as DropShadowFilter).inner)
                {
                    return this.cf[_loc_7];
                }
                if (flash.display:InteractiveObject == "DROP_SHADOW" && this.cf[_loc_7] is DropShadowFilter && !(this.cf[_loc_7] as DropShadowFilter).inner)
                {
                    return this.cf[_loc_7];
                }
                if (flash.display:InteractiveObject == "BEVEL" && this.cf[_loc_7] is BevelFilter)
                {
                    return this.cf[_loc_7];
                }
                if (flash.display:InteractiveObject == "INNER_GLOW" && this.cf[_loc_7] is GlowFilter && (this.cf[_loc_7] as GlowFilter).inner)
                {
                    return this.cf[_loc_7];
                }
                if (flash.display:InteractiveObject == "OUTER_GLOW" && this.cf[_loc_7] is GlowFilter && !(this.cf[_loc_7] as GlowFilter).inner)
                {
                    return this.cf[_loc_7];
                }
                _loc_7++;
            }
            switch(flash.display:InteractiveObject)
            {
                case "DROP_SHADOW":
                {
                    _loc_2 = new DropShadowFilter(10, 45, 0, 0.75, 0, 0, 1, 3, false);
                    this.cf.push(_loc_2);
                    return _loc_2;
                }
                case "INNER_SHADOW":
                {
                    _loc_3 = new DropShadowFilter(10, 45, 0, 0.75, 5, 5, 1, 3, true);
                    this.cf.push(_loc_3);
                    return _loc_3;
                }
                case "BEVEL":
                {
                    _loc_4 = new BevelFilter();
                    this.cf.push(_loc_4);
                    return _loc_4;
                }
                case "OUTER_GLOW":
                {
                    _loc_5 = new GlowFilter(4873348, 1, 6, 6, 2, 1, false);
                    this.cf.push(_loc_5);
                    return _loc_5;
                }
                case "INNER_GLOW":
                {
                    _loc_6 = new GlowFilter(4873348, 1, 6, 6, 2, 1, true);
                    this.cf.push(_loc_6);
                    return _loc_6;
                }
                default:
                {
                    return new BitmapFilter();
                    break;
                }
            }
        }// end function

        public function ChangeFilterState(event:ChangeEvent) : void
        {
            var _loc_2:* = event.currentTarget as CheckItem;
            if (!_loc_2.cb.checked)
            {
                this.RemoveFilter(_loc_2.name);
            }
            else
            {
                this.GetFilter(_loc_2.name);
            }
            this.FilterChange();
            return;
        }// end function

        override public function OKClick(event:MouseEvent) : void
        {
            ed.layers.Update();
            Close();
            return;
        }// end function

        override public function CloseClick(event:MouseEvent) : void
        {
            ed.canvas.layer.filters = this.bf;
            Close();
            return;
        }// end function

    }
}
